package com.mofang.powerdekorhelper.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.ButterKnife;

/**
 * Created by luofei on 2016/7/18 0018.
 */
public abstract class BaseFragment extends Fragment {

    protected Activity mContext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = initView(inflater);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public abstract View initView(LayoutInflater inflater);

    public abstract void initData(Bundle savedInstanceState);

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void initTitleBar(TitleBar mTitleBar, int titleId) {

        // 设置沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            // 透明状态栏
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            mTitleBar.setImmersive(true);
        } else {
            mContext.getWindow().setStatusBarColor(getResources().getColor(R.color.statusBar_bg7));
            mTitleBar.setImmersive(false);
        }
        mTitleBar.setBackgroundColor(getResources().getColor(R.color.white));
        mTitleBar.setTitle(getResources().getString(titleId));
        mTitleBar.setTitleSize(16);
        mTitleBar.setTitleColor(getResources().getColor(R.color.fontcolordeep));
    }

    public void toActivity(Class c) {
        startActivity(new Intent(mContext, c));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
